﻿

//
Proggy.ContentSort = function () {

    // reset input values
    var resetInputs = function () {

        // get inputs
        var theInputs = jQuery("div#proggy_sortable_children").find("input");

        //
        //console.log(theInputs);

        // iterate
        for (var i = 0; i < theInputs.size(); i++) {

            // get input
            var theInput = jQuery(theInputs[i]);

            // get name
            var theName = theInput.attr("name");

            // change the name
            var theName = theName.substring(0, theName.length - 2) + i + "]";
            //console.log(theName);

            // set
            theInput.attr("name", theName);
        }

        // 
        //console.log(theInputs);
    };

    // bind sort
    var bindSort = function () {

        // get div
        var sortableDiv = jQuery("div#proggy_sortable_children");

        // bind sort
        sortableDiv.sortable({

            items: "div.proggy-sort-child",
            axis: "y",
            opacity: 0.7,
            cursor: "move",
            stop: function (e, ui) {
                resetInputs();
            },
            tolerance: "pointer"
        });
    };

    // bind submit
    var bindSubmit = function () {

        // get buttons
        var form = jQuery("div.proggy-sort > form:first");

        // bind
        form.submit(function () {
            return confirm("Are you sure you want to re-order this content?\n\n" +
            "Your changes will reflect immediately in both live and draft content, " +
            "and all front-end content will be refreshed!");
        });
    };

    //
    this.Init = function () {

        // bind sort
        bindSort();

        // bind submit
        bindSubmit();

    };
};